#!coding=utf-8
from django.shortcuts import *
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect
from enterprise_basic_information.models import *
from util.log_util import triggerOpLog

@csrf_exempt
def login(request):
    if request.method == 'GET':
        return render_to_response('login.html')
    if request.method == 'POST':
        name = request.POST.get('name')
        pwd = request.POST.get('pwd')
        category = request.POST.get('category')
        if category=='1':
            user = User.objects.filter(username=name, password=pwd)
            if len(user) == 0:
                params={  'error' : "用户名或密码错误！",
                  }
                return render_to_response('login.html',params ,context_instance=RequestContext(request))
            else:
                request.session['user'] = user[0]
                _enterPrise= Enterprise.objects.get(user=user[0].id)
                request.session['ep']=_enterPrise
                park=_enterPrise.park
                enter_fill=Enter_fill.objects.filter(park=park).order_by('-create_at')
                if enter_fill:
                    fillModel=Fill.objects.get(enter_fill=enter_fill[0],enterprise=_enterPrise)
                    request.session['fill']=fillModel
                    return HttpResponseRedirect('/index2/')
                else:
                    return HttpResponseRedirect('/index2/')
        if category == '2':
            try:
                admin=User_admin.objects.get(username=name, password=pwd)
                request.session['admin'] = admin
                request.session['park']=admin.park
                triggerOpLog(u'园区用户登陆', u'%s园区管理员登陆了' % admin.username, admin.username)
                return HttpResponseRedirect('/park/mypark/')
            except Exception as e:
                return render_to_response('loginPark.html',{'error':"用户名和密码不正确！"})

def loginOut(request,type):
    if type == '1':
        return render_to_response('login.html')
    elif type == '2':
        admin = request.session['admin']
        request.session['admin'] = None
        
        triggerOpLog(u'园区用户退出', u'%s园区管理员退出了' % admin.username, admin.username)
        return render_to_response('loginPark.html')
    else:
        return render_to_response('login.html')

def index2(request):
    _enterPrise = request.session.get('ep')
    park=_enterPrise.park
    submitcount=0
    enter_fill=Enter_fill.objects.filter(park=park).order_by('-create_at')
    fillsession=request.session['fill']
    if fillsession:
        fillModel=request.session['fill']
        Applist=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise).order_by('order')
        for app in Applist:
            if app.app_status=='已确认':
                submitcount=submitcount+1

        if submitcount==10:
            if fillModel.status!='finish':
                fillModel.status='submit'
                fillModel.save()
                request.session['fill']=fillModel
        else:
            fillModel.status='no submit'
            fillModel.save()
            request.session['fill']=fillModel
        try:
            current=fillModel.enter_fill.fill_key
        except Exception as e:
            current=''
    else:
        fillModel=Fill.objects.get(enter_fill=enter_fill[0],enterprise=_enterPrise)
        Applist=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise).order_by('order')
        for app in Applist:
            if app.app_status=='已确认':
                submitcount=submitcount+1
        if submitcount==10:
            if fillModel.status!='finish':
                fillModel.status='submit'
                fillModel.save()
                request.session['fill']=fillModel
        else:
            fillModel.status='no submit'
            fillModel.save()
            request.session['fill']=fillModel
        current=enter_fill[0].fill_key
    param={'enter_fill':enter_fill,
           'current':current,
            'status':fillModel.status,
            'Applist':Applist
    }
    return render_to_response('index2.html',param,context_instance=RequestContext(request))

@csrf_exempt
def searchfillInCompany(request):
    fill=request.POST['fill']
    _enterPrise = request.session.get('ep')
    park=_enterPrise.park
    enter_fills=Enter_fill.objects.filter(park=park)
    enter_fill=Enter_fill.objects.get(park=park,fill_key=fill)
    fillModel=Fill.objects.get(enter_fill=enter_fill,enterprise=_enterPrise)
    Applist=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise).order_by('order')
    request.session['fill']=fillModel
    print request.session['fill']
    param={'enter_fill':enter_fills,
       'current':fill,
       'status':fillModel.status,
        'Applist':Applist}
    return render_to_response('index2.html',param,context_instance=RequestContext(request))

@csrf_exempt
def submitloginAmin(request):
    return HttpResponseRedirect('/admin/myadmin/')

def loginAdmin(request):
    return render_to_response('loginAdmin.html')

def parkDetail(request,id):
    park=Park.objects.get(id=id)
    params={'park':park}
    return render_to_response('admin/parkDetail.html',params,context_instance=RequestContext(request))

def parkEdit(request,id):
    user_admin=User_admin.objects.get(id=id)
    params={'user_admin':user_admin}
    return render_to_response('admin/parkEdit.html',params,context_instance=RequestContext(request))

def addPark(request):
    return render_to_response('admin/parkEdit.html',context_instance=RequestContext(request))


def parkDelete(request,id):
    park=Park.objects.get(id=id)
    User_admin.objects.filter(park=park).delete()
    Park.objects.filter(id=id).delete()
    return HttpResponseRedirect('/admin/myadmin/')

@csrf_exempt
def submitEidtPark(request):
    park_id=request.POST.get('park_id')
    name=request.POST.get('name')
    username=request.POST.get('username')
    password=request.POST.get('password')
    rank=request.POST.get('rank')
    create_time=request.POST.get('create_time')
    info=request.POST.get('info')
    production_type=request.POST.get('production_type')
    address=request.POST.get('address')
    area=request.POST.get('area')
    user_id=request.POST.get('user_id')
    if(park_id):
        park=Park.objects.get(id=park_id)
        park.name=name
        park.address=address
        park.rank=rank
        park.area=area
        park.info=info
        park.create_time=create_time
        park.production_type=production_type
        park.save()
        user_admin=User_admin.objects.get(id=user_id)
        user_admin.username=username
        user_admin.password=password
        user_admin.save()
    else:
        park=Park()
        park.name=name
        park.address=address
        park.rank=rank
        park.area=area
        park.info=info
        park.create_time=create_time
        park.production_type=production_type
        park.save()
        user_admin=User_admin()
        user_admin.username=username
        user_admin.password=password
        user_admin.park=park
        user_admin.save()
    return HttpResponseRedirect('/admin/myadmin/')

def parkList(request):
    user_admin=User_admin.objects.filter()
    params={'user_admin':user_admin}
    return render_to_response('admin/admin_detail.html',params,context_instance=RequestContext(request))

def myadmin(request):
    user_admin=User_admin.objects.filter()
    params={'user_admin':user_admin}
    return render_to_response('admin/admin_detail.html',params,context_instance=RequestContext(request))

def loginPark(request):
    return render_to_response('loginPark.html')

def loginSA(request):
    return render_to_response('loginSuperAdmin.html')

def register(request):
    park=Park.objects.filter()
    params={
        'park':park,
    }
    return render_to_response('register.html',params ,context_instance=RequestContext(request))

@csrf_exempt
def submitRegister(request):
    enterprise_name=request.POST.get('enterprise_name')
    legal_representative=request.POST.get('legal_representative')
    organization_code=request.POST.getlist('organization_code')
    registration_No=request.POST.getlist('registration_No')
    register_add0=request.POST.get('register_add0')
    register_add1=request.POST.get('register_add1')
    register_add2=request.POST.get('register_add2')
    register_add_detail=request.POST.get('register_add_detail')

    enterprise_type=request.POST.get('enterprise_type')
    industry_park_name=request.POST.get('industry_park_name')
    fill_people=request.POST.get('fill_people')
    mobile_phone=request.POST.get('mobile_phone')
    fax=request.POST.get('fax')
    mail=request.POST.get('mail')
    postal_code=request.POST.get('postal_code')
    username=request.POST.get('username')
    password=request.POST.get('password')

    organization_new=''
    for code in organization_code:
        organization_new=organization_new+code
    registration_new=''
    for code in registration_No:
        registration_new=code+registration_new

    user=User.objects.filter(username=username)
    params={
        'error':"用户名已经存在，请重新输入",
    }
    if(user):
        return render_to_response('register.html',params ,context_instance=RequestContext(request))
    else:
        user=User.objects.create(
        username=username,password=password
    )
    park=Park.objects.get(id=industry_park_name)
    enterprise=Enterprise()
    enterprise.name=enterprise_name
    enterprise.park=park
    enterprise.user=user
    '''
    默认为禁用
    '''
    enterprise.disable=1
    enterprise.save()
    enterprise_info=Enterprise_basic_information.objects.create(enterprise_name=enterprise_name,legal_representative=legal_representative,
                                                organization_code=organization_new,registration_No=registration_new,
                                                register_add0=register_add0,register_add1=register_add1,
                                                register_add2=register_add2,enterprise_type=enterprise_type,
                                                fill_people=fill_people,mobile_phone=mobile_phone,fax=fax,
                                                mail=mail,postal_code=postal_code,enterprise=enterprise,register_add_detail=register_add_detail)
    return render_to_response('login.html')

def submitAllMenuByFill(request):
    _enterPrise = request.session.get('ep')
    park=_enterPrise.park
    fillsession=request.session['fill']
    if fillsession:
        fillModel=request.session['fill']
        Applist=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise).order_by('order')
        fillModel.status='finish'
        fillModel.save()
        request.session['fill']=fillModel
        enter_fill=Enter_fill.objects.filter(park=park).order_by('-create_at')
        try:
            current=fillModel.enter_fill.fill_key
        except Exception as e:
            current=''
    param={'enter_fill':enter_fill,
           'current':current,
            'status':fillModel.status,
            'Applist':Applist
    }
    return render_to_response('index2.html',param,context_instance=RequestContext(request))
